<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Sect Index</title>
 	<%@ include file="/common/view/header.jsp"%>
  	<script>
  		var win=null, form=null, grid=null;
  		$(function(){
  			//create datagrid
			grid = $('#tt').treegrid({
                url:'<%=cp%>/rbac/getAllSect',
               	//pageSize:20,
    			//pageList:[5,10,15,20],
    			loadMsg:'正在加载系统资源列表......',
    			//pagination:true,
    			method:"post",
				height: document.body.clientHeight,
				rownumbers:true,
                fitColumns: true,
                singleSelect: true,
                treeField:"comments",
                idField:'id',
                columns:[[
			        {title:'资源名称',field:'comments',width:fixWidth(0.12),sortable:false,align:'left',
			        	formatter:function(value,row,index){
			        		if(row.group_id==null){
			        			return "<span style='font-weight:bold;font-size:15px'>"+value+"</span>";
			        		}else{
			        			return value;
			        		}
			        	}
			       	},
                	{title:'唯一URL定义',field:'sectname',width:fixWidth(0.16),sortable:false,align:'left'},
                	{title:'等级类别',field:'slevel',width:fixWidth(0.08),sortable:true,align:'center',
                		formatter:function(value,row,index){
                			return getSlevelName(value);
                		}
                	},
                	{title:'最后更新时间',field:'updatetime',width:fixWidth(0.08),sortable:false,align:'center'},
                	{title:'正序排序号',field:'sort',width:fixWidth(0.05),sortable:false,align:'center'},
                	{title:'操作',field:'doaction',align:'center',width:fixWidth(0.08),align:'center',
						formatter:function(value,row,index){
							return '<a href="javascript:void(-1);" onclick="deleteRow(\''+row.id+'\')">删除</a>'; 
						}
					}
				]],
                onLoadSuccess:function(){
                	for(var i=1; i<4; i++){
                		$('#tt').treegrid("mergeCells",{index:i,field:'comments',colspan:6});
                	}
                },
                onDblClickRow:editDataItem,
				toolbar:[{
						id:'btnadd',
						text:'新增资源',
						iconCls:'icon-add',
						handler:newDataItem
					},'-',{
						text:'编辑资源',
						iconCls:'icon-edit',
						handler:editDataItem
					},'-',{
						text:'展开全部',
						iconCls:'icon-reload',
						handler:function(){$('#tt').treegrid('expandAll');}
					},'-',{
						text:'关闭全部',
						iconCls:'icon-reload',
						handler:function(){$('#tt').treegrid('collapseAll');}
					},'-',{
						text:'显示全部',
						iconCls:'icon-reload',
						handler:displayAll
					},'-'
				]
            });
			//create window
			init_window();
			//create form
			init_form();
		});
  		/* 初始化窗口控件 */
  		function init_window(){
  			var width = 500;
  			var height = 400;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
  			win = $('#data_win').window({
				closed:true,
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: true,
				iconCls:'icon-app'
			});
  		}
  		/* 初始化表单控件 */
  		function init_form(){
  			form = win.find('form');
  		}
  		/* 新增数据项 */
  		function newDataItem(){
  			win.window('setTitle','添加新资源');
  			win.window('open');
			form.form('clear');
			form.url = '<%=cp%>/rbac/addSect';
  		}
  		/* 编辑数据项 */
  		function editDataItem(){
  			var row = grid.datagrid('getSelected');
  			if (row){
				if(row.group_id==null){
					$.messager.alert('警告',"该记录不能编辑。",'error');
					return;
				}
  				win.window('setTitle','更新资源');
				win.window('open');
				form.form('clear');
				form.form('load', '<%=cp%>/rbac/getSect/'+row.id+'?rnd='+Math.random());
				form.url = '<%=cp%>/rbac/editSect/'+row.id+'?rnd='+Math.random();
				
				//以下为 支持ie9需要给form中的select控件赋值
				$('#group_id').combobox('setValue',row.group_id);
				$('#slevel').combobox('setValue',row.slevel);
				$('#pid').combobox({
					url:'<%=cp%>/rbac/getParentSect/'+row.group_id+'/'+row.slevel,
					onLoadSuccess:function(){
						$('#pid').combobox("setValue",row.pid);
					}
				});
			} else {
				$.messager.show({
					title:'警告',
					msg:'请您先选择资源。'
				});
			}
  		}
  		/* 关闭表单窗口 */
  		function closeWindow(){
			if(win)
				win.window('close');
		}
  		/* 显示全部记录 */
  		function displayAll(){
  			grid.treegrid('reload');
  		}
  		/* 保存数据 */
  		function do_saveData(){
  			form.form('submit', {
				url:form.url,
				onSubmit:function(){  
					return $(this).form('validate');  
				},
				success:function(data){
					data = unescape(data);
					eval('data='+data);
					if (data.success){
						grid.treegrid('reload');
						closeWindow();
					} else {
						$.messager.alert('错误',data.msg,'error');
					}
				}
			});
  		}
  		/* 删除数据 */
  		function deleteRow(value){
  			if(value){
  				$.messager.confirm('提示','您是否确认执行删除操作？',function(r){  
					if(r){
						if(value != ""){
							var url = '<%=cp%>/rbac/delSect/'+value; 
							$.post(url,function(msg) {
									$.messager.show({title:'提示',msg:'系统资源项删除成功。',timeout:3000,showType:'fade'});
									grid.treegrid('reload');
								}
							);
						}
					}  
				});
  			}else{
  				$.messager.show({title:'警告',msg:'请您先选择资源。'});
  			}
		}
  		function selectMenu(rec){
  			var group_id = $("#group_id").combobox("getValue");
  			var slevel = $("#slevel").combobox("getValue");
  			if(group_id!="" && slevel!=""){
  				$('#pid').combobox({
					url:'<%=cp%>/rbac/getParentSect/'+group_id+'/'+slevel,
					onLoadSuccess:function(){}
				});
  			}
  		}
  	</script>
  </head>
  <body style="margin:0px;" id="c_body">
  	<!-- datagrid -->
	<table id="tt"></table>
	<!-- data item form -->
	<div id="data_win" class="easyui-window" closed="true" modal="true">  
	    <div style="text-align:center; padding:1px;">  
	        <form id="data_form" method="post">  
	            <table width="90%" cellspacing="1" cellpadding="2" border="0" align="center">
	                <tr>
	                    <td>所属系统：</td>
	                    <td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该资源分配所在系统" id="group_id" name="group_id" data-options="url:'<%=cp%>/rbac/getAllGroup',method:'get',textField:'title',valueField:'id',editable:false,onSelect:selectMenu" style="width:180px;"/></td>
	                </tr>
	                <tr>
	                    <td>资源等级：</td>
	                    <td>
	                      <select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该资源确定等级" id="slevel" name="slevel" data-options="editable:false,onSelect:selectMenu" style="width:120px;">
	                        <option value="0">无等级</option>
	                        <option value="1">应用级</option>
	                        <option value="2">模块级</option>
	                        <option value="3">操作级</option>
	                      </select>
	                    </td>
	                </tr>
	                <tr>
	                    <td>所属菜单组：</td>
	                    <td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该资源分配所在菜单组" id="pid" name="pid" data-options="textField:'comments',valueField:'id',groupField:'group',editable:false" style="width:120px;"/></td>
	                </tr>
	                <tr>
	                    <td>资源名称：</td>
	                    <td><input name="comments"></input></td>
	                </tr>
	                <tr>
	                    <td>唯一URL定义：</td>
	                    <td><input name="sectname" size="30"></input></td>
	                </tr>
	                <tr>
	                    <td>图标文件路径：</td>
	                    <td><input name="icon" size="30"></input></td>
	                </tr>
	                <tr>
	                    <td>正序排序号：</td>
	                    <td><input name="sort" value="0"></input></td>
	                </tr>
	                <tr>
	                    <td>备注说明：</td>
	                    <td><textarea name="remark" cols="30" rows="5" style="width:100%;"></textarea></td>
	                </tr>
	            </table>
	        </form>
	    </div>
	    <div style="text-align:center;padding:5px;">
	        <a href="javascript:void(0)" onclick="do_saveData()" id="btn-save" class="easyui-linkbutton" icon="icon-save">保存</a>  
	        <a href="javascript:void(0)" onclick="closeWindow()" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">取消</a>  
	    </div>  
	</div>
  </body>
</html>